package com.qianbei.share.ui;

import android.content.Intent;
import android.graphics.Color;
import android.graphics.drawable.ColorDrawable;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.view.LayoutInflater;
import android.view.MotionEvent;
import android.view.View;
import android.view.ViewGroup;
import android.view.Window;
import android.widget.AdapterView;
import android.widget.ListView;

import com.qianbei.R;
import com.qianbei.common.base.BaseActivity;
import com.qianbei.common.base.BaseFragmentDialog;
import com.qianbei.common.dialog.CommonMenuAdapter;
import com.qianbei.common.dialog.CommonQuestionDialog;
import com.qianbei.common.net.view.MsgAlert;
import com.qianbei.common.util.ConstantValue;
import com.qianbei.share.wx.WxShare;
import com.umeng.socialize.ShareAction;
import com.umeng.socialize.UMShareListener;
import com.umeng.socialize.bean.SHARE_MEDIA;
import com.umeng.socialize.media.UMImage;

import java.util.ArrayList;
import java.util.List;

public class NewShareFragmentDialog extends BaseFragmentDialog {

    private ListView mListView;
    private CommonMenuAdapter mAdapter;
    private List<Integer> mItems = new ArrayList<>();
    private CommonQuestionDialog.DialogItemOnClickListener mListener;

    private int mMenuTextColor;
    private int mMenuPosition = -1;

    private Object mClickData;

    private String mShareTitle;
    private String mShareSubTitle;
    private String mShareUrl;
    private String mShareImageUrl;


    public static NewShareFragmentDialog newInstance(String mShareTitle, String mShareSubTitle, String mShareUrl, String mShareImageUrl) {
        NewShareFragmentDialog f = new NewShareFragmentDialog();
        // Supply num input as an argument.
        Bundle args = new Bundle();
        args.putString("mShareTitle", mShareTitle);
        args.putString("mShareSubTitle", mShareSubTitle);
        args.putString("mShareUrl", mShareUrl);
        args.putString("mShareImageUrl", mShareImageUrl);
        f.setArguments(args);
        return f;
    }

    public static NewShareFragmentDialog newInstance(String mShareTitle, String mShareSubTitle, String mShareUrl, String mShareImageUrl,ArrayList<Integer> data) {
        NewShareFragmentDialog f = new NewShareFragmentDialog();
        // Supply num input as an argument.
        Bundle args = new Bundle();
        args.putString("mShareTitle", mShareTitle);
        args.putString("mShareSubTitle", mShareSubTitle);
        args.putString("mShareUrl", mShareUrl);
        args.putString("mShareImageUrl", mShareImageUrl);

        args.putIntegerArrayList(ConstantValue.DATA_KEY,data);
        f.setArguments(args);
        return f;
    }

    public static NewShareFragmentDialog newInstance(ArrayList<Integer> data) {

        Bundle args = new Bundle();
        args.putIntegerArrayList(ConstantValue.DATA_KEY,data);

        NewShareFragmentDialog fragment = new NewShareFragmentDialog();
        fragment.setArguments(args);
        return fragment;
    }


    public View onCreateView(LayoutInflater inflater,
                             @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        View view  = inflater.inflate(R.layout.common_question_dialog,container,false);

        getDialog().getWindow().setBackgroundDrawable(new ColorDrawable(Color.TRANSPARENT));
        getDialog().requestWindowFeature(Window.FEATURE_NO_TITLE);
        return view;
    }


    @Override
    public void onViewCreated(View view, Bundle savedInstanceState) {
        super.onViewCreated(view, savedInstanceState);

        if (getArguments() != null) {
            mShareTitle = getArguments().getString("mShareTitle");// ("typ");.
            mShareSubTitle = getArguments().getString("mShareSubTitle");
            mShareUrl = getArguments().getString("mShareUrl");
            mShareImageUrl = getArguments().getString("mShareImageUrl");

            mItems = getArguments().getIntegerArrayList(ConstantValue.DATA_KEY);
        }


        mListView = (ListView) view.findViewById(R.id.common_list_view);

        mAdapter = new CommonMenuAdapter(getActivity(),mItems, R.layout.dialog_item_common_question);
        mAdapter.setMenuTextColorByPosition(mMenuPosition,mMenuTextColor);
        mListView.setAdapter(mAdapter);

        mListView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {

                if (mListener != null){
                    mListener.dialogOnItemClick(mItems.get(position),mClickData);
                }else{
                    shareAction(mItems.get(position));
                }

                dismiss();
            }
        });

        view.setOnTouchListener(new View.OnTouchListener() {
            @Override
            public boolean onTouch(View v, MotionEvent event) {

                if (v instanceof ListView){
                    return false;
                }else{
                    dismiss();
                    return true;
                }
            }
        });
    }


    public void shareAction(int resId){
        switch (resId){

            case R.string.share_wei_xin:
                if (WxShare.getInstent().isWeixinAvilible()) {
                    shareWeiXin(SHARE_MEDIA.WEIXIN, mShareTitle, mShareSubTitle, mShareUrl, listener);
                } else {
                    new MsgAlert().show("请安装微信");
                }
                break;
            case R.string.share_friend:
                if (WxShare.getInstent().isWeixinAvilible()) {
                    shareWeiXin(SHARE_MEDIA.WEIXIN_CIRCLE, mShareTitle, mShareSubTitle, mShareUrl, listener);
                } else {
                    new MsgAlert().show("请安装微信");
                }
                break;
        }
    }

    /**
     * 微信分享
     *
     * @param shareMedia 分享到  朋友、朋友圈
     * @param title      分享title
     * @param subTitle   分享subtitle
     * @param url        分享url
     * @param listener   分享回调事件
     */
    public void shareWeiXin(SHARE_MEDIA shareMedia, String title, String subTitle, String url, UMShareListener listener) {
        UMImage image = new UMImage(mContext, mShareImageUrl);
        new ShareAction(mContext)
                .setPlatform(shareMedia)
                .setCallback(listener)
//                .withTitle(title)
                .withText(subTitle)
//                .withTargetUrl(url)
                .withMedia(image)
                .share();
    }


    private UMShareListener listener = new UMShareListener() {
        @Override
        public void onStart(SHARE_MEDIA share_media) {

        }

        @Override
        public void onResult(SHARE_MEDIA share_media) {
            if (share_media == SHARE_MEDIA.WEIXIN_CIRCLE) {
                new MsgAlert().show(R.string.share_success);
            }

        }

        @Override
        public void onError(SHARE_MEDIA share_media, Throwable throwable) {

        }

        @Override
        public void onCancel(SHARE_MEDIA share_media) {

        }
    };



}
